package com.itfitness.ftlib.widget.meituannav;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.PointF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * @ProjectName: FitnessLib
 * @Package: com.itfitness.ftlib.widget.meituannav
 * @ClassName: MeiTuanNavLayout
 * @Description: java类作用描述
 * @Author: 作者名
 * @CreateDate: 2020/11/26 21:22
 * @UpdateUser: 更新者：itfitness
 * @UpdateDate: 2020/11/26 21:22
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class MeiTuanNavLayout extends LinearLayout {
    private Path path = new Path();
    private float innerArcFloatHeight = 0;//最高的控制点的Y坐标
    private boolean isFloated = false;//是否已经凸起
    private ImageView imgChild;
    private ValueAnimator animatorFloat;
    private int imgChildWidth = 0;
    private float offsetFloatHeight = 10f;//动画的偏移高度
    private ValueAnimator animatorRestract;

    public MeiTuanNavLayout(Context context) {
        this(context,null);
    }

    public MeiTuanNavLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MeiTuanNavLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(VERTICAL);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
    }

    /**
     * 浮起
     */
    public void animFloat(){
        if(isFloated){
            return;
        }
        if(imgChild  == null || imgChildWidth <= 0){
            return;
        }
        initFloatAnim();
        isFloated = true;
        animatorFloat.start();
    }

    /**
     * 缩回
     */
    public void animRetract(){
        if(!isFloated){
            return;
        }
        if(imgChild  == null || imgChildWidth <= 0){
            return;
        }
        initRestractAnim();
        isFloated = false;
        animatorRestract.start();
    }

    /**
     * 加载缩回的动画
     */
    private void initRestractAnim() {
        if(animatorRestract == null){
            animatorRestract = ValueAnimator.ofFloat(imgChildWidth / 2f, 0);
            animatorRestract.setDuration(500);
            animatorRestract.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float animatedValue = (float) animation.getAnimatedValue();
                    if(imgChild != null){
                        imgChild.setTranslationY(-animatedValue/3);
                    }
                    setFloatHeight(animatedValue);
                }
            });
        }
    }

    /**
     * 加载浮起的动画
     */
    private void initFloatAnim() {
        if(animatorFloat == null){
            animatorFloat = ValueAnimator.ofFloat(0, imgChildWidth / 2f + offsetFloatHeight,imgChildWidth / 2f);
            animatorFloat.setDuration(500);
            animatorFloat.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float animatedValue = (float) animation.getAnimatedValue();
                    if(imgChild != null){
                        imgChild.setTranslationY(-animatedValue/3);
                    }
                    setFloatHeight(animatedValue);
                }
            });
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if(imgChild == null){
            //获取ImageView 和ImageView 的宽度
            for(int i = 0 ; i < getChildCount() ; i++){
                View child = getChildAt(i);
                if(child instanceof ImageView){
                    imgChild = (ImageView) child;
                    imgChildWidth = imgChild.getMeasuredWidth();
                    LinearLayout.LayoutParams layoutParams = (LayoutParams) imgChild.getLayoutParams();
                    layoutParams.topMargin += (imgChildWidth / 2 + offsetFloatHeight);//整体的高度等于自适应高度加浮动的高度加偏移的高度
                    imgChild.setLayoutParams(layoutParams);
                    Log.e("宽度",imgChildWidth+"");
                    break;
                }
            }
        }
    }

    @Override
    public void draw(Canvas canvas) {
        clicPath(canvas);
        super.draw(canvas);
    }
    public void setFloatHeight(float floatHeight){
        innerArcFloatHeight = floatHeight;
        postInvalidate();
    }
    private void clicPath(Canvas canvas) {
        path.reset();
        float centerY = imgChildWidth / 2f + offsetFloatHeight;
        float outArcWidth = imgChildWidth / 6f;
        float innerArcWidth = imgChildWidth + outArcWidth * 2;
        float contralYoffset = 0;//控制点的偏移
        if(innerArcFloatHeight >= imgChildWidth / 2f){
            contralYoffset = innerArcFloatHeight - imgChildWidth / 2f;
        }
        //第一段贝塞尔曲线的各个点
        PointF oneArcStartPoint = new PointF((getWidth() - innerArcWidth) / 2f - outArcWidth - contralYoffset ,centerY);
        PointF oneArcContralPoint = new PointF((getWidth() - innerArcWidth) / 2f ,centerY);
        PointF oneArcEndPoint = new PointF((getWidth() - innerArcWidth) / 2f + outArcWidth,centerY - innerArcFloatHeight /3);

        //第二段贝塞尔曲线的控制点（开始点是第一段贝塞尔曲线的结束点，结束点是第三段贝塞尔曲线的开始点）
        PointF twoArcContralPoint = new PointF(getWidth() / 2f, centerY - innerArcFloatHeight);

        //第三段贝塞尔曲线的各个点
        PointF threeArcStartPoint = new PointF((getWidth() - innerArcWidth) / 2f + innerArcWidth - outArcWidth,centerY - innerArcFloatHeight /3 );
        PointF threeArcContralPoint = new PointF((getWidth() - innerArcWidth) / 2f + innerArcWidth ,centerY);
        PointF threeArcEndPoint = new PointF((getWidth() - innerArcWidth) / 2f + innerArcWidth + outArcWidth + contralYoffset ,centerY );

        path.moveTo(0,centerY);
        path.lineTo(oneArcStartPoint.x,oneArcStartPoint.y);
        path.quadTo(oneArcContralPoint.x,oneArcContralPoint.y,oneArcEndPoint.x,oneArcEndPoint.y);
        path.quadTo(twoArcContralPoint.x,twoArcContralPoint.y,threeArcStartPoint.x,threeArcStartPoint.y);
        path.quadTo(threeArcContralPoint.x,threeArcContralPoint.y,threeArcEndPoint.x,threeArcEndPoint.y);
        path.lineTo(getWidth(),centerY);
        path.lineTo(getWidth(),getHeight());
        path.lineTo(0,getHeight());
        path.close();
        canvas.clipPath(path);
//        canvas.drawColor(Color.WHITE);
    }
}
